import pandas as pd
a=pd.read_csv('./as.csv',encoding='gbk',dtype='str')   #定义类型确保没有小数点
#处理数据
b=a.drop_duplicates(subset=['名称'])      #对数据去重
test=b.dropna(how='any',subset=['上映时间'],inplace=False)     #对上映时间去空值
c_ds=test.dropna(how='any',subset=['类型'],inplace=False)      #对类型去空值
c_d=test.dropna(how='any',subset=['更新时间'],inplace=False)      #对更新时间去空值
c=c_d.drop(c_d[(c_d['上映时间']=='19524') | (c_d['上映时间']=='19530') | (c_d['上映时间']=='19630') |
(c_d['上映时间']=='19637') | (c_d['上映时间']=='19900') |(c_d['上映时间']=='19991') | (c_d['上映时间']=='19994') | (c_d['上映时间']=='20014') |(c_d['上映时间']=='20117')
| (c_d['上映时间']=='20165') | (c_d['上映时间']=='20196') | (c_d['上映时间'] == '2021')] .index)

#聚类模型的构建,对c表上映时间处理
from sklearn.cluster import KMeans
import numpy as np
from sklearn.preprocessing import MinMaxScaler
nian=c['上映时间'].astype('int')   #转换成整型
c['更新时间']=pd.to_datetime(c['更新时间'])
fen_test=pd.DataFrame(c['更新时间'])
fen_y= pd.DataFrame([i.year for i in fen_test['更新时间']])
fen_m= pd.DataFrame([i.month for i in fen_test['更新时间']])
fen=pd.concat([100*fen_y+fen_m],axis=1,join='outer')
fen_test.shape
nianfens=pd.concat([pd.DataFrame(nian),fen],axis=1)
nianfens.dropna(how='any',subset=['上映时间'],inplace=True)      #对上映时间去空值
nianfens.dropna(how='any',subset=[0],inplace=True)      #对更新时间去空值
nianfen_t=nianfens.astype('int')
nianfen_t.columns=['上映时间','更新年月']
nianfen=nianfen_t.sort_values(['上映时间'],ascending=False)
scale = MinMaxScaler().fit(nianfen[-5000:])## 训练规则
iris = scale.transform(nianfen[-5000:]) ## 应用规则
kmeans = KMeans(n_clusters =2,random_state=123).fit(iris) ##构建并训练模型
result=kmeans.predict(nianfen[:5000])
print(result)
#聚类结果可视化
import pandas as pd
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
tsne=TSNE(n_components=2,init='random',random_state=123).fit(nianfen[-5000:])
df=pd.DataFrame(tsne.embedding_)
print(kmeans.labels_.shape)
df['labels']=kmeans.labels_
abc=kmeans.labels_
df1=df[df['labels']==0]
df2=df[df['labels']==1]
fig=plt.figure(figsize=(9,6))
plt.plot(df1[0],df1[1],'bo',df2[0],df2[1],'r+')
plt.show()

print("根据二个特征聚类后的聚类中心：",kmeans.cluster_centers_)
num = pd.Series(kmeans.labels_).value_counts()
print('每类用户数目为：\n',num)
#轮廓系数聚类评价
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
silhouettteScore = []
for i in range(2,10):
    kmeans = KMeans(n_clusters = i,random_state=123).fit(nianfen[-5000:])
    score = silhouette_score(nianfen[-5000:],kmeans.labels_)
    silhouettteScore.append(score)
plt.figure(figsize=(10,10))
plt.plot(range(2,10),silhouettteScore,linewidth=1.5, linestyle="-")
plt.show()